package com.scsj.supermarket.model;

import com.scsj.supermarket.base.CollectBase;
import com.scsj.supermarket.bean.BaseBean;
import com.scsj.supermarket.bean.MyCollectGoodsBean;
import com.scsj.supermarket.bean.SelectCollectByUserBean;

import dkmvp.net.ResponseCallback;
import dkmvp.net.ResponseHandler;
import dkmvp.net.RetryWithDelay;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 商品和店铺收藏查看，收藏和取消收藏Model
 */

public class CollectModel extends BaseModel implements CollectBase.IModel {

    /**
     * 查询收藏店铺的信息
     */
    @Override
    public Subscription getShopCollectData(RequestBody requestBody, final ResponseCallback<SelectCollectByUserBean> callback) {
        Observable<SelectCollectByUserBean> observable = service.SelectCollectByUser(requestBody);
        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<SelectCollectByUserBean>() {
                    @Override
                    public void call(SelectCollectByUserBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    /**
     * 店铺收藏和取消收藏
     */
    @Override
    public Subscription getShopAddAndDelData(RequestBody requestBody, final ResponseCallback<BaseBean> callback) {
        Observable<BaseBean> observable = service.StoreCollent(requestBody);
        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<BaseBean>() {
                    @Override
                    public void call(BaseBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    /**
     * 查询收藏商品的信息
     *
     * @param requestBody
     * @param callback
     * @return
     */
    public Subscription getGoodsCollectData(RequestBody requestBody, final ResponseCallback<MyCollectGoodsBean> callback) {
        Observable<MyCollectGoodsBean> observable = service.MyCollectGoods(requestBody);
        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<MyCollectGoodsBean>() {
                    @Override
                    public void call(MyCollectGoodsBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    /**
     * 用户添加收藏商品
     *
     * @param requestBody
     * @param callback
     * @return
     */
    public Subscription getGoodsAddCollectData(RequestBody requestBody, final ResponseCallback<BaseBean> callback) {
        Observable<BaseBean> observable = service.AddGoodsfavorite(requestBody);
        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<BaseBean>() {
                    @Override
                    public void call(BaseBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    /**
     * 用户删除收藏商品
     *
     * @param requestBody
     * @param callback
     * @return
     */
    public Subscription getGoodsDelCollectData(RequestBody requestBody, final ResponseCallback<BaseBean> callback) {
        Observable<BaseBean> observable = service.DelGoodsfavorite(requestBody);
        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<BaseBean>() {
                    @Override
                    public void call(BaseBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }
}
